package com.woniu.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.uuid.UUID;
import com.woniu.entity.Client;
import com.woniu.entity.CustomFeed;
import com.woniu.entity.User;
import com.woniu.entity.UserInfoEntity;
import com.woniu.service.CustomFeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/feed")
public class CustomFeedController extends BaseController {
    @Autowired
    CustomFeedService customFeedService;

    @GetMapping("/list")
    public TableDataInfo list(CustomFeed customFeed) {
        startPage();
        List<CustomFeed> list = customFeedService.findAllCustomFeed(customFeed);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('order:applicantList')")
    @GetMapping(value = "/{feedId}")
    public AjaxResult getInfo(@PathVariable int feedId) {
        return AjaxResult.success(customFeedService.selectFeedById(feedId));
    }


    @GetMapping("/addNewFeedS1")
    public AjaxResult addNewFeedS1(CustomFeed customFeed) {
        System.out.println(customFeed.toString());
        //后台数据校验
        HashSet<String> message = new HashSet<>();
        if (customFeed.getClientName() == null || customFeed.getClientName().trim() == null) {
            message.add("客户姓名是空的");
        }
        ;
        if (customFeed.getCitizenIdentity() == null || customFeed.getCitizenIdentity().trim() == null) {
            message.add("客户身份证号是空的");
        }
        ;
        if (customFeed.getProductionName() == null || customFeed.getProductionName().trim() == null) {
            message.add("产品未选");
        }
        ;
        if (customFeed.getPhone() == null || customFeed.getPhone().trim() == null) {
            message.add("手机号是空的");
        }
        ;
        if (customFeed.getChannelName() == null) {
            message.add("渠道未选");
        }
        ;

        if (message.size() != 0) {
            return AjaxResult.error("有非法表单数据", message);
        } else {

            return AjaxResult.success(customFeedService.addNewFeedS1(customFeed));
        }
    }
    @PostMapping(value = "/saveInfo",produces = "application/json")
    public AjaxResult saveInfoToClientTable(@RequestBody UserInfoEntity userInfo){
        //字符串压缩
//        String s1 = Base64.encodeBase64String(userInfo.getBytes(StandardCharsets.UTF_8));
//        System.out.println(s1);
        //字符串解压
//        String s2 = Base64.decodeBase64(s1);
//        System.out.println(s2);

        System.out.println("userInfo:"+userInfo);//前端将clientId塞进去？？？？？

        //将数据存入数据库中:e.g.存储到客户1中
        //TODO 需要从前端传递客户id
        //把客户信息封装进json中并转为字符串,其key设定为"jsonStr"
        if(customFeedService.saveUserInfo(userInfo)){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    @GetMapping("/findChannel")
    public AjaxResult findChannel() {
        return AjaxResult.success(customFeedService.findChannel());
    }

    @GetMapping("/findApplicantStatus")
    public AjaxResult findApplicantStatus() {
        return AjaxResult.success(customFeedService.findApplicantStatus());
    }

    @GetMapping("/findOrigin")
    public AjaxResult findOrigin() {
        return AjaxResult.success(customFeedService.findOrigin());
    }

    @GetMapping("/findPrincipal")
    public AjaxResult findPrincipal() {
        return AjaxResult.success(customFeedService.findPrincipal());
    }

    @PostMapping("/update")
    public AjaxResult UpdateFeedPrincipal(@RequestBody CustomFeed customFeed) {
        return AjaxResult.success(customFeedService.updateFeedPrincipal(customFeed));
    }

    @GetMapping("/findProduction/{channelName}")
    public AjaxResult findProduction(@PathVariable String channelName) {
        return AjaxResult.success(customFeedService.finProduction(channelName));
    }
    @GetMapping("/export")
    public TableDataInfo exportList() {
        startPage();
        List<CustomFeed> all = customFeedService.exportList();
        return getDataTable(all);
    }
    @PostMapping("/imgUpload")
    public AjaxResult imgUpload(MultipartFile file, HttpServletRequest req) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String originImgName = file.getOriginalFilename();

        String format = sdf.format(new Date());
        String realPath = req.getServletContext().getRealPath("/") + format;
        String newName = UUID.randomUUID().toString() + ".jpg";
        File folder = new File(realPath);
        //判断文件夹是否存在
        if (!folder.exists()) {
            folder.mkdirs();
        }

       //文件写入磁盘
        file.transferTo(new File(folder, newName));
        HashMap<String, String> map = new HashMap<>();
        map.put("key",realPath+File.separator+newName);
        return AjaxResult.success("上传成功", map);

    }
}
